/*
 * 给定一个正整数，从1到此数的连续整数序列中，求1出现的个数
 *
 */

#include <stdio.h>
#include <string.h>

//问题的解
int find_1_count_in_series(int num);

int find_1_count_in_series(int num)
{
    int number = 0;
    int low = 0;
    int high = 0;
    int cur = 0;
    int factor = 1;

    while (num / factor) {
       low = num - (num / factor) * factor;
       cur = (num / factor) % 10;
       high = num / (factor * 10);
       switch(cur) {
       case 0:
           number += high * factor;
           break;
       case 1:
           number += high * factor + 1 + low;
           break;
       default:
           number += (high + 1) * factor;
           break;
       }
       factor *= 10;
    }
    
    return number;
}

int main()
{
    int num = 0;
    printf("input a number: ");
    scanf("%d", &num);
    printf("%d\n", find_1_count_in_series(num));
}

